/*! \file
 * \brief Enter a brief description here.
 *
 * Enter a detailed description here. (optional)
 * \author Author (Ivo Wingelaar)
 * \date Date (2011)
 * \version Version (0.0.1)
 * \copyright Copyright (GNU LGPL v3)
 */

#include "LynxTimeManagerStub_C.hpp"

namespace Lynx
{

////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
TimeManagerStub_C::TimeManagerStub_C() :
    mVirtualTimerSpeed(1.0f),
    mVirtualTimerStopCounter(0),
    mLastVirtualTime(0),
    mStartRealTime(0),
    mStaticTime(0)
{
    //
}

////////////////////////////////////////////////////////////////////////////////
//! Virtual destructor.
TimeManagerStub_C::~TimeManagerStub_C()
{
    //
}

////////////////////////////////////////////////////////////////////////////////
//! Initializes the virtual timer.
void TimeManagerStub_C::init_virtual_timer(void)
{
    mStartRealTime = mStaticTime = get_real_time();
}

////////////////////////////////////////////////////////////////////////////////
//! Returns virtual time.
uint32_t TimeManagerStub_C::get_virtual_time(void) const
{
    if(is_virtual_clock_stopped())
        return mLastVirtualTime;

    return mLastVirtualTime + (uint32_t)((mStaticTime - mStartRealTime) * mVirtualTimerSpeed);
}

////////////////////////////////////////////////////////////////////////////////
//! Sets the virtual time.
void TimeManagerStub_C::set_virtual_time(uint32_t time)
{
    mStaticTime = get_real_time();
    mLastVirtualTime = time;
    mStartRealTime = mStaticTime;
}

////////////////////////////////////////////////////////////////////////////////
//! Returns the speed value of the virtual clock.
float TimeManagerStub_C::get_virtual_speed(void) const
{
    return mVirtualTimerSpeed;
}

///////////////////////////////////////////////////////////////////////////////
//! Sets the speed of the virtual clock.
void TimeManagerStub_C::set_virtual_speed(float speed)
{
    set_virtual_time(get_virtual_time());

    mVirtualTimerSpeed = speed;
    if(mVirtualTimerSpeed < 0.0f)
        mVirtualTimerSpeed = 0.0f;
}

////////////////////////////////////////////////////////////////////////////////
//! Stops the virtual clock.
void TimeManagerStub_C::stop_virtual_clock(void)
{
    if(!is_virtual_clock_stopped())
        mLastVirtualTime = get_virtual_time(); // Stop the virtual timer.

    --mVirtualTimerStopCounter;
}

////////////////////////////////////////////////////////////////////////////////
//! Starts the virtual clock.
void TimeManagerStub_C::start_virtual_clock(void)
{
    ++mVirtualTimerStopCounter;

    if(!is_virtual_clock_stopped())
        set_virtual_time(mLastVirtualTime); // Start the virtual timer.
}

////////////////////////////////////////////////////////////////////////////////
//! Returns if the clock if currently stopped.
bool TimeManagerStub_C::is_virtual_clock_stopped(void) const
{
    return mVirtualTimerStopCounter < 0;
}

////////////////////////////////////////////////////////////////////////////////
//! Increment the clock to reflect any changes.
void TimeManagerStub_C::tick(void)
{
    mStaticTime = get_real_time();
}

} // namespace Lynx
